package com.jackrain.nea.ad.service;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.ad.api.AduserattrDelCmd;
import com.jackrain.nea.ad.mapper.AduserattrMapper;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class AduserattrDelCmdImpl extends CommandAdapter implements AduserattrDelCmd {
    @Override
    public ValueHolder execute(QuerySession querySession) throws NDSException {
        ValueHolder valueHolder = new ValueHolder();
        AduserattrMapper aduserattrMapper = ApplicationContextHandle.getBean(AduserattrMapper.class);
        DefaultWebEvent event = querySession.getEvent();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(event.getParameterValue("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        Long objId;
        if (StringUtils.isNotEmpty(param.getString("objid")) && param.getLong("objid")>0){
            objId = param.getLong("objid");
            //查询此记录是否存在
            int idCount = aduserattrMapper.queryuserattrById(objId);
            if (idCount == 0) {
                throw new NDSException(Resources.getMessage("当前记录已不存在！", querySession.getLocale()));
            }

            int count = aduserattrMapper.deleteUserattrById(objId);

            //结果为0则说明字记录已被删除
            if (count==0){
                throw new NDSException(Resources.getMessage("当前记录已不存在！",querySession.getLocale()));
            }
            valueHolder.put("message",Resources.getMessage("删除成功的记录数：",querySession.getLocale())+count);
        }else {
            throw new NDSException(Resources.getMessage("请传入ID",querySession.getLocale()));
        }

        valueHolder.put("code",0);
        return valueHolder;
    }
}
